package com.vspuzzle;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.util.TypedValue;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.TextView;

class AnitmationTessera implements Runnable{
	
	Handler handler;
	final class_Game15 game15;
	final Map<Integer, Map<String,Integer> > matrice_text_views;
	final Gioco activity;
	final TextView tessera1; 
	final TextView tessera2; 
	final TextView tessera3; 
	
	final Map<String, Integer> arr1;
	final Map<String, Integer> arr2;
	final Map<String, Integer> arr3;
	final Map<String, Integer> arrz;
		
	long tot_time;
	long tot_dist;
	long perc_dist;
	long time_unit;
	int left;
	int top;
	
	boolean asse_x = false;
	
	public AnitmationTessera(Handler handler,Gioco activity,class_Game15 game15,Map<Integer, Map<String,Integer> > matrice_text_views,int num1,int num2,int num3,int arr1,int arr2,int arr3,int arrz,long time){
		this.handler = handler;
		this.activity = activity;
		
		this.tessera1 = (TextView) activity.findViewById(activity.get_id_view_tessera(num1));
		if (num2==-1) this.tessera2=null;
		else  this.tessera2 = (TextView) activity.findViewById(activity.get_id_view_tessera(num2));
		if (num3==-1) this.tessera3=null;
		else  this.tessera3 = (TextView) activity.findViewById(activity.get_id_view_tessera(num3));
		
		this.matrice_text_views = matrice_text_views;
		this.game15 = game15;
				
		this.arr1 = matrice_text_views.get(((TextView) activity.findViewById(activity.get_id_view_tessera(arr1))).getId());
		if(arr2==-1) this.arr2=null; else this.arr2=matrice_text_views.get(((TextView) activity.findViewById(activity.get_id_view_tessera(arr2))).getId());
		if(arr3==-1) this.arr3=null; else this.arr3=matrice_text_views.get(((TextView) activity.findViewById(activity.get_id_view_tessera(arr3))).getId());
		if(arrz==-1) this.arrz=null; else this.arrz=matrice_text_views.get(((TextView) activity.findViewById(activity.get_id_view_tessera(arrz))).getId());
		
		
		MarginLayoutParams m =  (MarginLayoutParams) this.tessera1.getLayoutParams();
		left = m.leftMargin;
	    top = m.topMargin;
		long x = Math.abs( this.arr1.get("leftMargin") - m.leftMargin );
		long y = Math.abs( this.arr1.get("topMargin") -m.topMargin );
	    tot_time = time;
	    if(x>y){
	    	asse_x = true;
	    	tot_dist = x;
	    }else{
	    	tot_dist = y;
	    }
	    	    
	    Resources r = activity.getResources();
	    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
	    int pixels = (int) Math.round(px);
	    tot_dist = tot_dist/pixels;// qui ho lo spazio che devo percorrere in dp
	    
	    if(tot_dist<=0){//Tessera si trova gia' in pos finale non servono animazioni
			m.leftMargin = this.arr1.get("leftMargin");
			m.topMargin = this.arr1.get("topMargin");
			this.tessera1.setLayoutParams(m);
			if(tessera2!=null){
				MarginLayoutParams m2 =  (MarginLayoutParams) this.tessera2.getLayoutParams();
				m2.leftMargin = this.arr2.get("leftMargin");
				m2.topMargin = this.arr2.get("topMargin");
				this.tessera2.setLayoutParams(m2);
				if(tessera3!=null){
					MarginLayoutParams m3 =  (MarginLayoutParams) this.tessera3.getLayoutParams();
					m3.leftMargin = this.arr3.get("leftMargin");
					m3.topMargin = this.arr3.get("topMargin");
					this.tessera3.setLayoutParams(m3);
				}
			}
	    }else{
	    	if(tot_time<=tot_dist){
		    	perc_dist = tot_dist/tot_time;
		    	time_unit = 1;
		    }else{
		    	time_unit = tot_time/tot_dist;
		    	perc_dist = pixels;
		    }
		    if(this.arr1.get("leftMargin") < m.leftMargin || 
		       this.arr1.get("topMargin") < m.topMargin) perc_dist = -perc_dist;
		    
	    	Thread t = new Thread(this,"animation");
			t.start();
	    }
	    
		
		if(arrz != -1){
			final TextView zero = (TextView) activity.findViewById(R.id.tessera_0);
			MarginLayoutParams mz = (MarginLayoutParams) zero.getLayoutParams();
			mz.leftMargin = this.arrz.get("leftMargin");
			mz.topMargin = this.arrz.get("topMargin");
			zero.setLayoutParams(mz);
			
			this.game15.matrice_gioco[this.arr1.get("y")][this.arr1.get("x")] = num1;
			this.game15.matrice_gioco[this.arrz.get("y")][this.arrz.get("x")] = 0;
			if(num2!=-1) this.game15.matrice_gioco[this.arr2.get("y")][this.arr2.get("x")] = num2;
			if(num3!=-1) this.game15.matrice_gioco[this.arr3.get("y")][this.arr3.get("x")] = num3;
			
			matrice_text_views.remove(this.tessera1.getId());
			Map<String,Integer> data = new HashMap<String, Integer>();
			data.put("x", this.arr1.get("x"));
			data.put("y", this.arr1.get("y"));
			data.put("topMargin", this.arr1.get("topMargin"));
			data.put("leftMargin", this.arr1.get("leftMargin"));
			matrice_text_views.put(this.tessera1.getId(), data);
			
			matrice_text_views.remove(R.id.tessera_0);
			data = new HashMap<String, Integer>();
			data.put("x", this.arrz.get("x"));
			data.put("y", this.arrz.get("y"));
			data.put("topMargin", this.arrz.get("topMargin"));
			data.put("leftMargin", this.arrz.get("leftMargin"));
			matrice_text_views.put(R.id.tessera_0, data);
			
			if(num2!=-1){
				this.game15.matrice_gioco[this.arr2.get("y")][this.arr2.get("x")] = num2;
				matrice_text_views.remove(this.tessera2.getId());
				data = new HashMap<String, Integer>();
				data.put("x", this.arr2.get("x"));
				data.put("y", this.arr2.get("y"));
				data.put("topMargin", this.arr2.get("topMargin"));
				data.put("leftMargin", this.arr2.get("leftMargin"));
				matrice_text_views.put(this.tessera2.getId(), data);
				if(num3!=-1){
					this.game15.matrice_gioco[this.arr3.get("y")][this.arr3.get("x")] = num3;
					matrice_text_views.remove(this.tessera3.getId());
					data = new HashMap<String, Integer>();
					data.put("x", this.arr3.get("x"));
					data.put("y", this.arr3.get("y"));
					data.put("topMargin", this.arr3.get("topMargin"));
					data.put("leftMargin", this.arr3.get("leftMargin"));
					matrice_text_views.put(this.tessera3.getId(), data);
				}
			}
		}
		
	}
	
	@Override
	public void run() {
		MarginLayoutParams m = (MarginLayoutParams) this.tessera1.getLayoutParams();
		while(tot_time>0){
		    
			if(asse_x) left  = left + (int)perc_dist;
			else	top =  top + (int)perc_dist;
			tot_time = tot_time - time_unit;
			
			int neg=1;
			if(perc_dist<0) neg=-1;
			
			if(arrz==null) neg=-neg;
			
			if(tessera2!=null){
				int sumleft = (asse_x) ? left+(neg*this.tessera2.getWidth()) : arr2.get("leftMargin") ;
				int sumtop = (asse_x) ?  arr2.get("topMargin") : top+(neg*this.tessera2.getHeight()) ;
				int [] message = {this.tessera2.getId(),sumleft,sumtop};
				notifyMessage(message);
				if(tessera3!=null){
					int sumleft3 = (asse_x) ? sumleft+(neg*this.tessera3.getWidth()) : arr3.get("leftMargin") ;
					int sumtop3 = (asse_x) ?  arr3.get("topMargin") : sumtop+(neg*this.tessera3.getHeight()) ;
					int [] message3 = {this.tessera3.getId(),sumleft3,sumtop3};
					notifyMessage(message3);
					
				}
			}
			int [] message = {this.tessera1.getId(),left,top};
			notifyMessage(message);
			
			
			try{
				Thread.sleep(time_unit);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		
		}
		int [] message = {this.tessera1.getId(),arr1.get("leftMargin"),arr1.get("topMargin")};
		notifyMessage(message);
		if(tessera2!=null){
			int [] message2 = {this.tessera2.getId(),this.arr2.get("leftMargin"),this.arr2.get("topMargin")};
			notifyMessage(message2);
			if(tessera3!=null){
				int [] message3 = {this.tessera3.getId(),this.arr3.get("leftMargin"),this.arr3.get("topMargin")};
				notifyMessage(message3);
			}
		}
	}

	
	private void notifyMessage(int [] str) {
	    Message msg = handler.obtainMessage();
	    Bundle b = new Bundle();
	    b.putIntArray("move_text_view", str);
	    
	    msg.setData(b);
	    handler.sendMessage(msg);
	  }

}
/*

class AnitmationTessera extends AsyncTask<Void, TextView, Void>{
	
	final TextView view; 
	final class_Game15 game15;
	final Map<Integer, Map<String,Integer> > matrice_text_views;
	final Activity activity;
	final TextView tessera1; 
	final TextView tessera2; 
	final TextView tessera3; 
	final Map<String, Integer> data_numero1;
	
	float veloctia;
	
	long tot_time;
	float perc_dist;
	
	boolean asse_x = false;
	
	public AnitmationTessera(Activity activity,class_Game15 game15,Map<Integer, Map<String,Integer> > matrice_text_views,final TextView view,TextView idt1, TextView idt2, TextView idt3,float veloctia){
		this.activity = activity;
		this.view = view;
		this.tessera1 = idt1;
		this.tessera2 = idt2;
		this.tessera3 = idt3;
		this.matrice_text_views = matrice_text_views;
		this.game15 = game15;
		
		this.veloctia = veloctia;
		
		data_numero1 = matrice_text_views.get(tessera1.getId());
		
		MarginLayoutParams m =  (MarginLayoutParams) this.view.getLayoutParams();
		float x = data_numero1.get("leftMargin")-m.leftMargin;
		float y = data_numero1.get("topMargin") -m.topMargin;
	    tot_time = (long) (10000/this.veloctia);
	    if(x>y){
	    	asse_x = true;
	    	perc_dist = x;
	    }else{
	    	perc_dist = y;
	    }
	    float pd = perc_dist/tot_time;
	    if(pd<1){
	    	tot_time = (long) (tot_time / perc_dist);
	    }else perc_dist = pd;
		final Map<String, Integer> data_numero = matrice_text_views.get(this.view.getId());
				
		final TextView zero = (TextView) activity.findViewById(R.id.tessera_0);
				
		MarginLayoutParams mz = (MarginLayoutParams) zero.getLayoutParams();
		mz.leftMargin = data_numero.get("leftMargin");
		mz.topMargin = data_numero.get("topMargin");
		zero.setLayoutParams(mz);
		Thread t = new Thread(this,"animation");
		t.start();
	}
	
	public void run() {
		
	}

	@Override
	protected Void doInBackground(Void... params) {
		MarginLayoutParams m = null;
		while(tot_time>0){
			LayoutParams p = this.view.getLayoutParams();
		    m =  (MarginLayoutParams) p;
			
			if(asse_x)  m.leftMargin = m.leftMargin + (int)perc_dist;
			else	m.topMargin =  m.topMargin + (int)perc_dist;
			final MarginLayoutParams params = m;
			tot_time--;
			
			((TextView) activity.findViewById(this.view.getId())).post(new Runnable() {
				
				@Override
				public void run() {
					int a = 1+1;
					a++;
					view.setLayoutParams(params);
				}
			});
			
			SystemClock.sleep(1);
		
		}
		m.leftMargin = data_numero1.get("leftMargin");
		m.topMargin = data_numero1.get("topMargin");
		this.view.setLayoutParams(m);
		
		publishProgress(item); 
		
		return null;
	}
	
	@Override
	protected void onProgressUpdate(TextView... item) {
		
	}

}*/
